


export const bus = {
  events: {},
  on(name,fn){
    if(this.events[name]){
      this.events[name].push(fn)
    }else{
      this.events[name] = [fn]
    }
  },

  off(name,fn){
     if(this.events[name]){
      let index = this.events[name].indexOf(fn)
      this.events[name].splice(index,1)
    }
  },

  emit(name){
    if(this.events[name]){
            // 创建副本，如果回调函数内又在name下面注册了事件，会造成死循环
      let callbackFn = this.events[name].slice();
      callbackFn.forEach(fn=>{
        fn()
      })
    }
  },

 // 触发某个时间，只触发一次
  emit(name,once){
    if(this.events[name]){
            // 创建副本，如果回调函数内又在name下面注册了事件，会造成死循环
      let callbackFn = this.events[name].slice();
      callbackFn.forEach(fn=>{
        fn()
      })
    }
    if(once){
       delete this.events[name]
      }
    }
  }

